#include "gtest/gtest.h"

#include <iostream>

#include "TimerTask.h"
#include  "AsioTimerTask.h"

using namespace frame::imp;


class Task : public ITimerTask {
public:
    Task(TimerTasks * timerTasks)
    :timerTasks(timerTasks){
    }
    ~Task()override{
        timerTasks = nullptr;
    }
    void handle(Timer * timer) override{
    }
private:
    TimerTasks * timerTasks;
};

/* 
TEST(TimerTaskTest, use){
    TimerTasks timerTasks;
    Task task(&timerTasks);

    timerTasks.insertNoFind(new Timer(1, & task, 11));

    ASSERT_TRUE(timerTasks.contains(1));
    ASSERT_EQ(1, timerTasks.size());

    ASSERT_FALSE(timerTasks.insert(new Timer(1, & task, 10)));
    ASSERT_TRUE(timerTasks.insert(new Timer(2, & task, 12)));

    std::cout << *(timerTasks.getTimer(2)) << std::endl;

    timerTasks.remove(1);
    ASSERT_EQ(1, timerTasks.size());

    for(auto & p : timerTasks){
        std::cout << p.first << " " << *(p.second) << std::endl;
    }
}*/



using namespace frame::imp::asio;
TEST(AsioTimerTaskTest, use){
    boost::asio::io_context ioContext;
    AsioTimerTasks att(ioContext);
    Task task(nullptr);
    
    att.insertNoFind(1, &task, 11);
    ASSERT_TRUE(att.contains(1));
    ASSERT_EQ(1, att.size());

    ASSERT_FALSE(att.insert(1, & task, 10));
    ASSERT_TRUE(att.insert(2, & task, 12));

    std::cout << *(att.getTimer(2)) << std::endl;

    att.remove(1);
    ASSERT_EQ(1, att.size());

    for(auto & p : att){
        std::cout << p.first << " " << *(p.second) << std::endl;
    }
}

TEST(AsioTimer, sharedTest){
    boost::asio::io_context ioContext;
    std::function<void ()> func;
    {
        std::shared_ptr<AsioTimer> atn = std::make_shared<AsioTimer>(1, nullptr,100, ioContext);
        func = atn->TESTStart();
    }
    func();
}

TEST(AsioTimerTasks, sharedTest){
    boost::asio::io_context ioContext;
    AsioTimerTasks att(ioContext);
    
    att.insertNoFind(1, nullptr, 11);

    
    std::function<void ()> func;
    for(auto & pair : att){
        std::shared_ptr<AsioTimer> atn = std::static_pointer_cast<AsioTimer>(pair.second);
        func  = atn->TESTStart();
    }

    att.remove(1);
    ASSERT_EQ(0, att.size());

    func();
}
